package twilight.companion.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.registries.ForgeRegistries;
import twilight.companion.entity.SummonSkullEntity;

public class SummonSkullRenderer extends EntityRenderer<SummonSkullEntity> {
    private final ItemRenderer itemRenderer;
    
    public SummonSkullRenderer(EntityRendererProvider.Context context) {
        super(context);
        this.itemRenderer = context.getItemRenderer();
    }
    
    @Override
    public void render(SummonSkullEntity entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
        poseStack.pushPose();

        ResourceLocation trophyLocation = new ResourceLocation("twilightforest", "alpha_yeti_trophy");
        ItemStack skull = new ItemStack(ForgeRegistries.ITEMS.getValue(trophyLocation));
        
        if (!skull.isEmpty()) {
            float rotationSpeed = entity.getRotationSpeed();
            float currentRotation = (entity.tickCount + partialTicks) * rotationSpeed;

            poseStack.mulPose(Axis.YP.rotationDegrees(currentRotation));
            poseStack.mulPose(Axis.XP.rotationDegrees((float)Math.sin(Math.toRadians(currentRotation * 2)) * 15.0F));
            poseStack.mulPose(Axis.ZP.rotationDegrees((float)Math.cos(Math.toRadians(currentRotation * 1.5)) * 10.0F));

            float scale = 1.5F + (float)Math.sin(entity.tickCount * 0.1F) * 0.2F;
            poseStack.scale(scale, scale, scale);

            itemRenderer.renderStatic(skull, ItemDisplayContext.GROUND, packedLight, OverlayTexture.NO_OVERLAY, poseStack, buffer, entity.level(), entity.getId());
        }
        
        poseStack.popPose();
        super.render(entity, entityYaw, partialTicks, poseStack, buffer, packedLight);
    }
    
    @Override
    public ResourceLocation getTextureLocation(SummonSkullEntity entity) {
        return ResourceLocation.tryParse("twilightforest:textures/item/alpha_yeti_trophy.png");
    }
}